namespace User; 

use Zend\Mvc\ModuleRouteListener;
use Zend\Mvc\MvcEvent;

class Module
{
    public function onBootstrap(MvcEvent $e)
    {
        $eventManager        = $e->getApplication()->getEventManager();
        $moduleRouteListener = new ModuleRouteListener();
        $moduleRouteListener->attach($eventManager);
    }

    public function getConfig()
    {
        return include __DIR__ . '/config/module.config.php';
    }

    public function getAutoloaderConfig()
    {
        return array(
            'Zend\Loader\StandardAutoloader' => array(
                'namespaces' => array(
                    __NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
                ),
            ),
        );
    }

    protected $strNameSpace = 'User\Model';
    protected $tableFactoryMap = array(
        'user_info'   => 'UserInfo',
    );

    public function getServiceConfig()           //自己写的ORM
    {

        $factories = array();
        foreach($this->tableFactoryMap as $tableName => $modelName){
            $strModelTable = sprintf("%s\%sTable", $this->strNameSpace, $modelName);
            $strModel      = sprintf("%s\%s", $this->strNameSpace, $modelName);

            $strGateway = $modelName.'Gateway';
            $factories[$strModelTable] = function($sm) use($modelName,$strModelTable){
                $tableGateway = $sm->get($modelName.'Gateway');
                $table = new $strModelTable($tableGateway);
                return $table;
            };
            $factories[$strGateway] = function($sm) use($modelName,$tableName,$strModel) {
                $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
                $resultSetPrototype = new ResultSet();
                $resultSetPrototype->setArrayObjectPrototype(new $strModel());
                return new TableGateway($tableName,$dbAdapter,null,$resultSetPrototype);
            };
        }

        return array(
            'factories' => $factories,
        );
    }

}
